package com.easysoft.framework.workflow.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.workflow.mapper.SysWorkflowMapperExtd;
import com.easysoft.framework.workflow.model.SysWorkflow;
import com.easysoft.framework.workflow.model.SysWorkflowQuery;
import com.easysoft.framework.workflow.model.SysWorkflowView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("service.SysWorkflowService")
public class SysWorkflowService {
    @Autowired
    @Qualifier("mapper.SysWorkflowMapperExtd")
    private SysWorkflowMapperExtd sysWorkflowMapperExtd;

    @DataSource(DataSource.SLAVE)
    public List<SysWorkflowView> selectSysWorkflowByPage(Page page, SysWorkflowQuery query) {
        int maxRow = sysWorkflowMapperExtd.selectCountAllWorkflow(query);
        if (maxRow == 0){
            return new ArrayList<SysWorkflowView>();
        }
        page.setMaxRow(maxRow);
        return sysWorkflowMapperExtd.selectSysWorkflowByPage(page,query);
    }

    @DataSource(DataSource.MASTER)
    public int addSysWorkflow(SysWorkflow sysWorkflow) {
        sysWorkflow.setId(UUIDUtils.getUUID());
        sysWorkflow.setCreateTime(new Date());
        return sysWorkflowMapperExtd.insertSelective(sysWorkflow);
    }

    @DataSource(DataSource.SLAVE)
    public SysWorkflow selectSysWorkflowById(String id) {
        return sysWorkflowMapperExtd.selectByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateSysWorkflow(SysWorkflow sysWorkflow){
        return sysWorkflowMapperExtd.updateByPrimaryKeySelective(sysWorkflow);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysWorkflow(String id) {

        return sysWorkflowMapperExtd.deleteByPrimaryKey(id);
    }
}
